<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="迎着朝阳的博客" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    java面试题之数据库篇 |  迎着朝阳
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="https://dxysun.com/static/yan.png" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  
<script>
var _hmt = _hmt || [];
(function() {
	var hm = document.createElement("script");
	hm.src = "https://hm.baidu.com/hm.js?aa994a8d65700b8835787dd39d079d7e";
	var s = document.getElementsByTagName("script")[0]; 
	s.parentNode.insertBefore(hm, s);
})();
</script>


</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-javaForInterviewSQL"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  java面试题之数据库篇
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2018/07/16/javaForInterviewSQL/" class="article-date">
  <time datetime="2018-07-16T10:49:33.000Z" itemprop="datePublished">2018-07-16</time>
</a>   
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.9k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">9 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <p>总结一下java面试题-数据库篇</p>
<a id="more"></a>
<p>收集一些常见类型的SQL语句，无论对于平常开发还是准备面试，都会有助益。<br>基本表结构</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">student(sno,sname,sage,ssex)学生表</span><br><span class="line">course(cno,cname,tno) 课程表</span><br><span class="line">sc(sno,cno,score) 成绩表</span><br><span class="line">teacher(tno,tname) 教师表</span><br></pre></td></tr></table></figure>

<h3 id="查询课程1的成绩比课程2的成绩高的所有学生的学号"><a href="#查询课程1的成绩比课程2的成绩高的所有学生的学号" class="headerlink" title="查询课程1的成绩比课程2的成绩高的所有学生的学号"></a>查询课程1的成绩比课程2的成绩高的所有学生的学号</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> a.sno <span class="keyword">from</span></span><br><span class="line">(<span class="keyword">select</span> sno,score <span class="keyword">from</span> sc <span class="keyword">where</span> cno=<span class="number">1</span>) a,</span><br><span class="line">(<span class="keyword">select</span> sno,score <span class="keyword">from</span> sc <span class="keyword">where</span> cno=<span class="number">2</span>) b</span><br><span class="line"><span class="keyword">where</span> a.score&gt;b.score <span class="keyword">and</span> a.sno=b.sno</span><br></pre></td></tr></table></figure>

<h3 id="查询平均成绩大于60分的同学的学号和平均成绩"><a href="#查询平均成绩大于60分的同学的学号和平均成绩" class="headerlink" title="查询平均成绩大于60分的同学的学号和平均成绩"></a>查询平均成绩大于60分的同学的学号和平均成绩</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> a.sno <span class="keyword">as</span> <span class="string">"学号"</span>, <span class="keyword">avg</span>(a.score) <span class="keyword">as</span> <span class="string">"平均成绩"</span></span><br><span class="line"><span class="keyword">from</span></span><br><span class="line">(<span class="keyword">select</span> sno,score <span class="keyword">from</span> sc) a</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> sno <span class="keyword">having</span> <span class="keyword">avg</span>(a.score)&gt;<span class="number">60</span></span><br></pre></td></tr></table></figure>

<h3 id="查询所有同学的学号、姓名、选课数、总成绩"><a href="#查询所有同学的学号、姓名、选课数、总成绩" class="headerlink" title="查询所有同学的学号、姓名、选课数、总成绩"></a>查询所有同学的学号、姓名、选课数、总成绩</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> a.sno <span class="keyword">as</span> 学号, b.sname <span class="keyword">as</span> 姓名,</span><br><span class="line"><span class="keyword">count</span>(a.cno) <span class="keyword">as</span> 选课数, <span class="keyword">sum</span>(a.score) <span class="keyword">as</span> 总成绩</span><br><span class="line"><span class="keyword">from</span> sc a, student b</span><br><span class="line"><span class="keyword">where</span> a.sno = b.sno</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> a.sno, b.sname</span><br></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> student.sno <span class="keyword">as</span> 学号, student.sname <span class="keyword">as</span> 姓名,</span><br><span class="line"><span class="keyword">count</span>(sc.cno) <span class="keyword">as</span> 选课数, <span class="keyword">sum</span>(score) <span class="keyword">as</span> 总成绩</span><br><span class="line"><span class="keyword">from</span> student <span class="keyword">left</span> <span class="keyword">Outer</span> <span class="keyword">join</span> sc <span class="keyword">on</span> student.sno = sc.sno</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> student.sno, sname</span><br></pre></td></tr></table></figure>

<h3 id="查询姓“张”的老师的个数"><a href="#查询姓“张”的老师的个数" class="headerlink" title="查询姓“张”的老师的个数"></a>查询姓“张”的老师的个数</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">count</span>(<span class="keyword">distinct</span>(tname)) <span class="keyword">from</span> teacher <span class="keyword">where</span> tname <span class="keyword">like</span> <span class="string">'张%‘</span></span><br></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> tname <span class="keyword">as</span> <span class="string">"姓名"</span>, <span class="keyword">count</span>(<span class="keyword">distinct</span>(tname)) <span class="keyword">as</span> <span class="string">"人数"</span></span><br><span class="line"><span class="keyword">from</span> teacher</span><br><span class="line"><span class="keyword">where</span> tname <span class="keyword">like</span><span class="string">'张%'</span></span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> tname</span><br></pre></td></tr></table></figure>
<h3 id="查询没学过“张三”老师课的同学的学号、姓名"><a href="#查询没学过“张三”老师课的同学的学号、姓名" class="headerlink" title="查询没学过“张三”老师课的同学的学号、姓名"></a>查询没学过“张三”老师课的同学的学号、姓名</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> student.sno,student.sname <span class="keyword">from</span> student</span><br><span class="line"><span class="keyword">where</span> sno <span class="keyword">not</span> <span class="keyword">in</span> (<span class="keyword">select</span> <span class="keyword">distinct</span>(sc.sno) <span class="keyword">from</span> sc,course,teacher</span><br><span class="line"><span class="keyword">where</span> sc.cno=course.cno <span class="keyword">and</span> teacher.tno=course.tno <span class="keyword">and</span> teacher.tname=<span class="string">'张三'</span>)</span><br></pre></td></tr></table></figure>

<h3 id="查询同时学过课程1和课程2的同学的学号、姓名"><a href="#查询同时学过课程1和课程2的同学的学号、姓名" class="headerlink" title="查询同时学过课程1和课程2的同学的学号、姓名"></a>查询同时学过课程1和课程2的同学的学号、姓名</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sno, sname <span class="keyword">from</span> student</span><br><span class="line"><span class="keyword">where</span> sno <span class="keyword">in</span> (<span class="keyword">select</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> sc.cno = <span class="number">1</span>)</span><br><span class="line"><span class="keyword">and</span> sno <span class="keyword">in</span> (<span class="keyword">select</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> sc.cno = <span class="number">2</span>)</span><br></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">selectc.sno, c.sname from</span><br><span class="line">(<span class="keyword">select</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> sc.cno = <span class="number">1</span>) a,</span><br><span class="line">(<span class="keyword">select</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> sc.cno = <span class="number">2</span>) b,</span><br><span class="line">student c</span><br><span class="line"><span class="keyword">where</span> a.sno = b.sno <span class="keyword">and</span> a.sno = c.sno</span><br></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> student.sno,student.sname <span class="keyword">from</span> student,sc <span class="keyword">where</span> student.sno=sc.sno <span class="keyword">and</span> sc.cno=<span class="number">1</span></span><br><span class="line"><span class="keyword">and</span> <span class="keyword">exists</span>( <span class="keyword">select</span> * <span class="keyword">from</span> sc <span class="keyword">as</span> sc_2 <span class="keyword">where</span> sc_2.sno=sc.sno <span class="keyword">and</span> sc_2.cno=<span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<h3 id="查询学过“李四”老师所教所有课程的所有同学的学号、姓名"><a href="#查询学过“李四”老师所教所有课程的所有同学的学号、姓名" class="headerlink" title="查询学过“李四”老师所教所有课程的所有同学的学号、姓名"></a>查询学过“李四”老师所教所有课程的所有同学的学号、姓名</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> a.sno, a.sname <span class="keyword">from</span> student a, sc b</span><br><span class="line"><span class="keyword">where</span> a.sno = b.sno <span class="keyword">and</span> b.cno <span class="keyword">in</span></span><br><span class="line">(<span class="keyword">select</span> c.cno <span class="keyword">from</span> course c, teacher d <span class="keyword">where</span> c.tno = d.tno <span class="keyword">and</span> d.tname = <span class="string">'李四'</span>)</span><br></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> a.sno, a.sname <span class="keyword">from</span> student a, sc b,</span><br><span class="line">(<span class="keyword">select</span> c.cno <span class="keyword">from</span> course c, teacher d <span class="keyword">where</span> c.tno = d.tno <span class="keyword">and</span> d.tname = <span class="string">'李四'</span>) e</span><br><span class="line"><span class="keyword">where</span> a.sno = b.sno <span class="keyword">and</span> b.cno = e.cno</span><br></pre></td></tr></table></figure>

<h3 id="查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号、姓名"><a href="#查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号、姓名" class="headerlink" title="查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号、姓名"></a>查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号、姓名</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> a.sno, a.sname <span class="keyword">from</span> student a,</span><br><span class="line">(<span class="keyword">select</span> sno, score <span class="keyword">from</span> sc <span class="keyword">where</span> cno = <span class="number">1</span>) b,</span><br><span class="line">(<span class="keyword">select</span> sno, score <span class="keyword">from</span> sc <span class="keyword">where</span> cno = <span class="number">2</span>) c</span><br><span class="line"><span class="keyword">where</span> b.score &gt; c.score <span class="keyword">and</span> b.sno = c.sno <span class="keyword">and</span> a.sno = b.sno</span><br></pre></td></tr></table></figure>

<h3 id="查询所有课程成绩小于60分的同学的学号、姓名"><a href="#查询所有课程成绩小于60分的同学的学号、姓名" class="headerlink" title="查询所有课程成绩小于60分的同学的学号、姓名"></a>查询所有课程成绩小于60分的同学的学号、姓名</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sno,sname <span class="keyword">from</span> student</span><br><span class="line"><span class="keyword">where</span> sno <span class="keyword">not</span> <span class="keyword">in</span> (<span class="keyword">select</span> <span class="keyword">distinct</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> score &gt; <span class="number">60</span>)</span><br></pre></td></tr></table></figure>

<h3 id="查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名"><a href="#查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名" class="headerlink" title="查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名"></a>查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> a.sno, a.sname</span><br><span class="line"><span class="keyword">from</span> student a, sc b</span><br><span class="line"><span class="keyword">where</span> a.sno &lt;&gt; <span class="number">1</span> <span class="keyword">and</span> a.sno=b.sno <span class="keyword">and</span></span><br><span class="line">b.cno <span class="keyword">in</span> (<span class="keyword">select</span> cno <span class="keyword">from</span> sc <span class="keyword">where</span> sno = <span class="number">1</span>)</span><br></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> s.sno,s.sname</span><br><span class="line"><span class="keyword">from</span> student s,</span><br><span class="line">(<span class="keyword">select</span> sc.sno</span><br><span class="line"><span class="keyword">from</span> sc</span><br><span class="line"><span class="keyword">where</span> sc.cno <span class="keyword">in</span> (<span class="keyword">select</span> sc1.cno <span class="keyword">from</span> sc sc1 <span class="keyword">where</span> sc1.sno=<span class="number">1</span>) <span class="keyword">and</span> sc.sno&lt;&gt;<span class="number">1</span></span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> sc.sno) r1</span><br><span class="line"><span class="keyword">where</span> r1.sno=s.sno</span><br></pre></td></tr></table></figure>

<h3 id="把“sc”表中“王五”所教课的成绩都更改为此课程的平均成绩"><a href="#把“sc”表中“王五”所教课的成绩都更改为此课程的平均成绩" class="headerlink" title="把“sc”表中“王五”所教课的成绩都更改为此课程的平均成绩"></a>把“sc”表中“王五”所教课的成绩都更改为此课程的平均成绩</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">update sc set score &#x3D; (select avg(sc_2.score) from sc sc_2 where sc_2.cno&#x3D;sc.cno)</span><br><span class="line">from course,teacher where course.cno&#x3D;sc.cno and course.tno&#x3D;teacher.tno andteacher.tname&#x3D;&#39;王五&#39;</span><br></pre></td></tr></table></figure>

<h3 id="查询和编号为2的同学学习的课程完全相同的其他同学学号和姓名"><a href="#查询和编号为2的同学学习的课程完全相同的其他同学学号和姓名" class="headerlink" title="查询和编号为2的同学学习的课程完全相同的其他同学学号和姓名"></a>查询和编号为2的同学学习的课程完全相同的其他同学学号和姓名</h3><p>这一题分两步查：<br>1、</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sno</span><br><span class="line"><span class="keyword">from</span> sc</span><br><span class="line"><span class="keyword">where</span> sno &lt;&gt; <span class="number">2</span></span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> sno</span><br><span class="line"><span class="keyword">having</span> <span class="keyword">sum</span>(cno) = (<span class="keyword">select</span> <span class="keyword">sum</span>(cno) <span class="keyword">from</span> sc <span class="keyword">where</span> sno = <span class="number">2</span>)</span><br></pre></td></tr></table></figure>
<p>2、</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> b.sno, b.sname</span><br><span class="line"><span class="keyword">from</span> sc a, student b</span><br><span class="line"><span class="keyword">where</span> b.sno &lt;&gt; <span class="number">2</span> <span class="keyword">and</span> a.sno = b.sno</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> b.sno, b.sname</span><br><span class="line"><span class="keyword">having</span> <span class="keyword">sum</span>(cno) = (<span class="keyword">select</span> <span class="keyword">sum</span>(cno) <span class="keyword">from</span> sc <span class="keyword">where</span> sno = <span class="number">2</span>)</span><br></pre></td></tr></table></figure>

<h3 id="删除学习“王五”老师课的sc表记录"><a href="#删除学习“王五”老师课的sc表记录" class="headerlink" title="删除学习“王五”老师课的sc表记录"></a>删除学习“王五”老师课的sc表记录</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> sc <span class="keyword">from</span> course, teacher</span><br><span class="line"><span class="keyword">where</span> course.cno = sc.cno <span class="keyword">and</span> course.tno = teacher.tno <span class="keyword">and</span> tname = <span class="string">'王五'</span></span><br></pre></td></tr></table></figure>

<h3 id="向sc表中插入一些记录，这些记录要求符合以下条件："><a href="#向sc表中插入一些记录，这些记录要求符合以下条件：" class="headerlink" title="向sc表中插入一些记录，这些记录要求符合以下条件："></a>向sc表中插入一些记录，这些记录要求符合以下条件：</h3><p>将没有课程3成绩同学的该成绩补齐, 其成绩取所有学生的课程2的平均成绩</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> sc <span class="keyword">select</span> sno , (<span class="keyword">select</span> <span class="keyword">avg</span>(score) <span class="keyword">from</span> sc <span class="keyword">where</span> cno = <span class="number">2</span>)</span><br><span class="line"><span class="keyword">from</span> student</span><br><span class="line"><span class="keyword">where</span> sno <span class="keyword">not</span> <span class="keyword">in</span> (<span class="keyword">select</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> cno = <span class="number">3</span>)</span><br></pre></td></tr></table></figure>

<h3 id="按平均分从高到低显示所有学生的如下统计报表："><a href="#按平均分从高到低显示所有学生的如下统计报表：" class="headerlink" title="按平均分从高到低显示所有学生的如下统计报表："></a>按平均分从高到低显示所有学生的如下统计报表：</h3><p>— 学号,企业管理,马克思,UML,数据库,物理,课程数,平均分</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> sno <span class="keyword">as</span> 学号</span><br><span class="line">,<span class="keyword">max</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">1</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">AS</span> 企业管理</span><br><span class="line">,<span class="keyword">max</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">2</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">AS</span> 马克思</span><br><span class="line">,<span class="keyword">max</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">3</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">AS</span> UML</span><br><span class="line">,<span class="keyword">max</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">4</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">AS</span> 数据库</span><br><span class="line">,<span class="keyword">max</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">5</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">AS</span> 物理</span><br><span class="line">,<span class="keyword">count</span>(cno) <span class="keyword">AS</span> 课程数</span><br><span class="line">,<span class="keyword">avg</span>(score) <span class="keyword">AS</span> 平均分</span><br><span class="line"><span class="keyword">FROM</span> sc</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">by</span> sno</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">by</span> <span class="keyword">avg</span>(score) <span class="keyword">DESC</span></span><br></pre></td></tr></table></figure>

<h3 id="查询各科成绩最高分和最低分"><a href="#查询各科成绩最高分和最低分" class="headerlink" title="查询各科成绩最高分和最低分"></a>查询各科成绩最高分和最低分</h3><p>以如下形式显示：课程号，最高分，最低分</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> cno <span class="keyword">as</span> 课程号, <span class="keyword">max</span>(score) <span class="keyword">as</span> 最高分, <span class="keyword">min</span>(score) 最低分</span><br><span class="line"><span class="keyword">from</span> sc <span class="keyword">group</span> <span class="keyword">by</span> cno</span><br></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span>  course.cno <span class="keyword">as</span> <span class="string">'课程号'</span></span><br><span class="line">,<span class="keyword">MAX</span>(score) <span class="keyword">as</span> <span class="string">'最高分'</span></span><br><span class="line">,<span class="keyword">MIN</span>(score) <span class="keyword">as</span> <span class="string">'最低分'</span></span><br><span class="line"><span class="keyword">from</span> sc,course</span><br><span class="line"><span class="keyword">where</span> sc.cno=course.cno</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> course.cno</span><br></pre></td></tr></table></figure>

<h3 id="按各科平均成绩从低到高和及格率的百分数从高到低顺序"><a href="#按各科平均成绩从低到高和及格率的百分数从高到低顺序" class="headerlink" title="按各科平均成绩从低到高和及格率的百分数从高到低顺序"></a>按各科平均成绩从低到高和及格率的百分数从高到低顺序</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> t.cno <span class="keyword">AS</span> 课程号,</span><br><span class="line"><span class="keyword">max</span>(course.cname)<span class="keyword">AS</span> 课程名,</span><br><span class="line"><span class="keyword">isnull</span>(<span class="keyword">AVG</span>(score),<span class="number">0</span>) <span class="keyword">AS</span> 平均成绩,</span><br><span class="line"><span class="number">100</span> * <span class="keyword">SUM</span>(<span class="keyword">CASE</span> <span class="keyword">WHEN</span> <span class="keyword">isnull</span>(score,<span class="number">0</span>)&gt;=<span class="number">60</span> <span class="keyword">THEN</span> <span class="number">1</span> <span class="keyword">ELSE</span> <span class="number">0</span> <span class="keyword">END</span>)/<span class="keyword">count</span>(<span class="number">1</span>) <span class="keyword">AS</span> 及格率</span><br><span class="line"><span class="keyword">FROM</span> sc t, course</span><br><span class="line"><span class="keyword">where</span> t.cno = course.cno</span><br><span class="line"><span class="keyword">GROUP</span> <span class="keyword">BY</span> t.cno</span><br><span class="line"><span class="keyword">ORDER</span> <span class="keyword">BY</span> 及格率 <span class="keyword">desc</span></span><br></pre></td></tr></table></figure>

<h3 id="查询如下课程平均成绩和及格率的百分数-用”1行”显示"><a href="#查询如下课程平均成绩和及格率的百分数-用”1行”显示" class="headerlink" title="查询如下课程平均成绩和及格率的百分数(用”1行”显示):"></a>查询如下课程平均成绩和及格率的百分数(用”1行”显示):</h3><p>企业管理（001），马克思（002），UML （003），数据库（004）</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span></span><br><span class="line"><span class="keyword">avg</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">1</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">as</span> 平均分<span class="number">1</span>,</span><br><span class="line"><span class="keyword">avg</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">2</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">as</span> 平均分<span class="number">2</span>,</span><br><span class="line"><span class="keyword">avg</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">3</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">as</span> 平均分<span class="number">3</span>,</span><br><span class="line"><span class="keyword">avg</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">4</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">as</span> 平均分<span class="number">4</span>,</span><br><span class="line"><span class="number">100</span> * <span class="keyword">sum</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">1</span> <span class="keyword">and</span> score &gt; <span class="number">60</span> <span class="keyword">then</span> <span class="number">1</span> <span class="keyword">else</span> <span class="number">0</span> <span class="keyword">end</span>) / <span class="keyword">sum</span>(casewhen cno = <span class="number">1</span> <span class="keyword">then</span> <span class="number">1</span> <span class="keyword">else</span> <span class="number">0</span> <span class="keyword">end</span>) <span class="keyword">as</span> 及格率<span class="number">1</span>,</span><br><span class="line"><span class="number">100</span> * <span class="keyword">sum</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">2</span> <span class="keyword">and</span> score &gt; <span class="number">60</span> <span class="keyword">then</span> <span class="number">1</span> <span class="keyword">else</span> <span class="number">0</span> <span class="keyword">end</span>) / <span class="keyword">sum</span>(casewhen cno = <span class="number">2</span> <span class="keyword">then</span> <span class="number">1</span> <span class="keyword">else</span> <span class="number">0</span> <span class="keyword">end</span>) <span class="keyword">as</span> 及格率<span class="number">2</span>,</span><br><span class="line"><span class="number">100</span> * <span class="keyword">sum</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">3</span> <span class="keyword">and</span> score &gt; <span class="number">60</span> <span class="keyword">then</span> <span class="number">1</span> <span class="keyword">else</span> <span class="number">0</span> <span class="keyword">end</span>) / <span class="keyword">sum</span>(casewhen cno = <span class="number">3</span> <span class="keyword">then</span> <span class="number">1</span> <span class="keyword">else</span> <span class="number">0</span> <span class="keyword">end</span>) <span class="keyword">as</span> 及格率<span class="number">3</span>,</span><br><span class="line"><span class="number">100</span> * <span class="keyword">sum</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">4</span> <span class="keyword">and</span> score &gt; <span class="number">60</span> <span class="keyword">then</span> <span class="number">1</span> <span class="keyword">else</span> <span class="number">0</span> <span class="keyword">end</span>) / <span class="keyword">sum</span>(casewhen cno = <span class="number">4</span> <span class="keyword">then</span> <span class="number">1</span> <span class="keyword">else</span> <span class="number">0</span> <span class="keyword">end</span>) <span class="keyword">as</span> 及格率<span class="number">4</span></span><br><span class="line"><span class="keyword">from</span> sc</span><br></pre></td></tr></table></figure>

<h3 id="查询不同老师所教不同课程平均分-从高到低显示"><a href="#查询不同老师所教不同课程平均分-从高到低显示" class="headerlink" title="查询不同老师所教不同课程平均分, 从高到低显示"></a>查询不同老师所教不同课程平均分, 从高到低显示</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">max</span>(c.tname) <span class="keyword">as</span> 教师, <span class="keyword">max</span>(b.cname) 课程, <span class="keyword">avg</span>(a.score) 平均分</span><br><span class="line"><span class="keyword">from</span> sc a, course b, teacher c</span><br><span class="line"><span class="keyword">where</span> a.cno = b.cno <span class="keyword">and</span> b.tno = c.tno</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> a.cno</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> 平均分 <span class="keyword">desc</span></span><br></pre></td></tr></table></figure>
<p>或者</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> r.tname <span class="keyword">as</span> <span class="string">'教师'</span>,r.rname <span class="keyword">as</span> <span class="string">'课程'</span> , <span class="keyword">AVG</span>(score) <span class="keyword">as</span> <span class="string">'平均分'</span></span><br><span class="line"><span class="keyword">from</span> sc,</span><br><span class="line">(<span class="keyword">select</span>  t.tname,c.cno <span class="keyword">as</span> rcso,c.cname <span class="keyword">as</span> rname</span><br><span class="line"><span class="keyword">from</span> teacher t ,course c</span><br><span class="line"><span class="keyword">where</span> t.tno=c.tno)r</span><br><span class="line"><span class="keyword">where</span> sc.cno=r.rcso</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> sc.cno,r.tname,r.rname</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> <span class="keyword">AVG</span>(score) <span class="keyword">desc</span></span><br></pre></td></tr></table></figure>

<h3 id="查询如下课程成绩均在第3名到第6名之间的学生的成绩："><a href="#查询如下课程成绩均在第3名到第6名之间的学生的成绩：" class="headerlink" title="查询如下课程成绩均在第3名到第6名之间的学生的成绩："></a>查询如下课程成绩均在第3名到第6名之间的学生的成绩：</h3><p>— [学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> top <span class="number">6</span> <span class="keyword">max</span>(a.sno) 学号, <span class="keyword">max</span>(b.sname) 姓名,</span><br><span class="line"><span class="keyword">max</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">1</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">as</span> 企业管理,</span><br><span class="line"><span class="keyword">max</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">2</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">as</span> 马克思,</span><br><span class="line"><span class="keyword">max</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">3</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">as</span> UML,</span><br><span class="line"><span class="keyword">max</span>(<span class="keyword">case</span> <span class="keyword">when</span> cno = <span class="number">4</span> <span class="keyword">then</span> score <span class="keyword">end</span>) <span class="keyword">as</span> 数据库,</span><br><span class="line"><span class="keyword">avg</span>(score) <span class="keyword">as</span> 平均分</span><br><span class="line"><span class="keyword">from</span> sc a, student b</span><br><span class="line"><span class="keyword">where</span> a.sno <span class="keyword">not</span> <span class="keyword">in</span></span><br><span class="line"></span><br><span class="line">(<span class="keyword">select</span> top <span class="number">2</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> cno = <span class="number">1</span> <span class="keyword">order</span> <span class="keyword">by</span> score <span class="keyword">desc</span>)</span><br><span class="line"><span class="keyword">and</span> a.sno <span class="keyword">not</span> <span class="keyword">in</span> (<span class="keyword">select</span> top <span class="number">2</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> cno = <span class="number">2</span> <span class="keyword">order</span> <span class="keyword">by</span> scoredesc)</span><br><span class="line"><span class="keyword">and</span> a.sno <span class="keyword">not</span> <span class="keyword">in</span> (<span class="keyword">select</span> top <span class="number">2</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> cno = <span class="number">3</span> <span class="keyword">order</span> <span class="keyword">by</span> scoredesc)</span><br><span class="line"><span class="keyword">and</span> a.sno <span class="keyword">not</span> <span class="keyword">in</span> (<span class="keyword">select</span> top <span class="number">2</span> sno <span class="keyword">from</span> sc <span class="keyword">where</span> cno = <span class="number">4</span> <span class="keyword">order</span> <span class="keyword">by</span> scoredesc)</span><br><span class="line"><span class="keyword">and</span> a.sno = b.sno</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> a.sno</span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=https://dxysun.com/2018/07/16/javaForInterviewSQL/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/" rel="tag">java</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2018/07/16/javaForInterviewMutil/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            java面试题之多线程篇
          
        </div>
      </a>
    
    
      <a href="/2018/07/16/javaForInterviewFramework/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">java面试题之框架篇</div>
      </a>
    
  </nav>

  
   
  
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2024
        <i class="ri-heart-fill heart_icon"></i> dxysun
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
        <li>
          <a href="https://beian.miit.gov.cn" target="_black" rel="nofollow">豫ICP备17012675号-1</a>
        </li>
        
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="https://dxysun.com/static/logo.png" alt="迎着朝阳"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/photos">相册</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/alipay-20201219151322.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="https://tu.dxysun.com/weixin-20201219151346.png">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script>
  var ayerConfig = {
    mathjax: true
  }
</script>

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


<script src="/js/dz.js"></script>



    
  </div>
</body>

</html>